<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:include="include :: header"></head>
<body class="hold-transition skin-blue sidebar-mini">

<!--创建模态框-->
<div class="modal fade" id="modal-create">
    <div class="modal-dialog" style="width: 800px;">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">添加</h4>
            </div>
            <div class="modal-body">
                <form id="createForm" class="form-horizontal" role="form">
                    <input type="hidden" name="cosId">
                    <div class="form-group">
                        <label class="col-sm-3 control-label">项目编号</label>
                        <div class="col-sm-7">
                            <input type="text" autocomplete="off" class="form-control" name="cosProCode"
                                   placeholder="请输入项目编号">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">项目名称</label>
                        <div class="col-sm-7">
                            <input type="text" autocomplete="off" class="form-control" name="cosProName"
                                   placeholder="请输入项目名称">
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-info" id="btn-create">提交</button>
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
            </div>
        </div>
    </div>
</div>

<section class="content-header">
    <h1>
        成本中心维护
    </h1>
</section>
<section class="content">
    <div class="row">
        <div class="col-xs-12">
            <div class="box">
                <div class="box-header">
                    <form id="queryForm" onsubmit="return false;">
                        <div class="row">
                            <div class="form-inline">
                                <div class="form-group" style="margin-right: 10px;">
                                    <label>项目编号</label>
                                    <input type="text" name="cosProCode" class="form-control" placeholder="请输入项目编号">
                                </div>
                                <div class="form-group" style="margin-right: 10px;">
                                    <label>项目名称</label>
                                    <input type="text" name="cosProName" class="form-control" placeholder="请输入项目名称">
                                </div>
                                <button type="button" class="btn btn-success" id="btn-search">
                                    <!--<i class="fa fa-search"></i>-->
                                    <span class="Bold">搜索</span>
                                </button>
                                <a href="javascript:void(0);" shiro:haspermission="basic:cost:edit" class="btn btn-info pull-right"
                                   onclick="openCreate()">
                                    <!--<i class="fa fa-plus"></i>-->
                                    <span class="Bold">添加</span>
                                </a>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="box-body">
                    <table id="bootstrap-table"></table>
                </div>
            </div>
        </div>
    </div>
</section>
<div th:include="include :: footer"></div>
<script th:inline="javascript">

    var table;

    /**
     * 打开添加模态框
     */
    function openCreate() {
        js.reset("createForm", false);
        js.modal.open("modal-create");
    }

    /**
     * 打开模态框, 回显数据
     */
    function openEdit(id) {
        js.post({
            url: ctx + "basic/cost/getInfo",
            data: {
                cosId: id
            },
            success: function (result) {
                $("#createForm").fillData(result.data);
                js.modal.open("modal-create");
                $("#createForm [name='cosId']").val(result.data.cosId).trigger("change");
                js.modal.closeLoading();
                setTimeout(function () {
                    js.validate.validing("createForm");
                }, 500);
            }
        })
    }

    /**
     * 删除
     */
    function del(id) {
        js.modal.confirm("确认删除此成本信息吗？", function (result) {
            js.post({
                url: ctx + "basic/cost/del",
                data: {
                    cosId: id,
                },
                success: function (result) {
                    if (result.type === web_status.SUCCESS) {
                        js.modal.success(result.msg);
                        js.table.refresh(table);
                    } else {
                        js.modal.warning(result.msg);
                    }
                    js.modal.closeLoading();
                }
            })
        })
    }

    $(function () {

        /**
         * 加载表格
         */
        table = js.table.init({
            url: ctx + "basic/cost/list",
            queryForm: "queryForm",
            showExport: false,
            columns: [
                {
                    title: '序号', field: 'cosId', formatter: function (value, row, index) {
                        return '<p>' + (index + 1) + '</p>';
                    }
                },
                {
                    title: "项目号", field: 'cosProCode', align: "left", width: '20%',
                    formatter: function (value, row, index) {
                        return '<i class="fa fa-map-o"></i> <span class="nav-label">' + value + '</span>';
                    }
                },

                {title: '项目名称', field: 'cosProName'},
                {
                    title: "操作",
                    width: '250',
                    formatter: function (value, row, index) {
                        var actions = [];
                        if (hasP('basic:cost:edit')) {
                            actions.push('<a class="btn btn-warning btn-xs" href="javascript:void(0)" onclick="openEdit(' + row.cosId + ')"> 编辑</a> ');
                            actions.push('<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="del(' + row.cosId + ')"> 删除</a>');
                        }
                        return actions.join('');
                    }
                }
            ]

        });

        /**
         * 表格搜索
         */
        $("#btn-search").click(function () {
            js.table.search(table);
        });

        /**
         * 验证
         */
        js.validate.init("createForm", {
            fields: {
                cosProCode: {
                    validators: {
                        notEmpty: {message: '请输入项目编号'},
                        threshold: 6, //有6字符以上才发送ajax请求，（input中输入一个字符，插件会向服务器发送一次，设置限制，6字符以上才开始）
                        callback: {
                            message: "项目号不存在",
                            callback: function (value, validator) {
                                var flag = false;
                                $.ajax({
                                    url: ctx + 'purchase/change/codeExist', // 请求路径
                                    type: 'POST',
                                    async: false,	// 同步请求，必须是false
                                    data: {
                                        ceoNeedProCode: $("#createForm input[name='cosProCode']").val() // 请求参数
                                    },
                                    success: function (res) {
                                        flag = $.parseJSON(res).valid;
                                    }
                                });
                                return flag;
                            }
                        }
                    }
                },
                cosProName: {validators: {notEmpty: {message: '请输入项目名称'}}},
            }
        });

        /**
         * 创建成本信息
         */
        $("#btn-create").click(function () {
            var url = ctx + "basic/cost/save";
            if (js.validate.isValid("createForm")) {
                js.post({
                    url: url,
                    data: $("#createForm").serialize(),
                    success: function (result) {
                        if (result.type === web_status.SUCCESS) {
                            js.modal.success(result.msg);
                            js.table.refresh(table);
                            js.modal.hide("modal-create");
                        } else {
                            js.modal.warning(result.msg);
                        }
                        js.modal.closeLoading();
                    }
                })
            }
        });

        /**
         * 模态框隐藏事件， 重置表单，重置验证
         */
        js.modal.hideEvent("modal-create", function () {
            js.validate.reset("createForm");
            js.reset("createForm");
        })
        $("title").html($("title").html()+" —— 成本中心维护");
    });

</script>
</body>
</html>
